﻿using System;

namespace Bookvar.UI.Commands
{
    public static class BookvarCommands
    {
        private static readonly int commandsCount = Enum.GetNames( typeof ( CommandId ) ).Length;
        private static readonly BookvarCommand[] internalCommands = new BookvarCommand[commandsCount];

        public static BookvarCommand AddNewTopic
        {
            get { return EnsureCommand( CommandId.AddNewTopic ); }
        }

        public static BookvarCommand AddNewSubtopic
        {
            get { return EnsureCommand( CommandId.AddNewSubtopic ); }
        }

        public static BookvarCommand AddNewFloatingTopic
        {
            get { return EnsureCommand( CommandId.AddNewFloatingTopic ); }
        }

        public static BookvarCommand DeleteTopic
        {
            get { return EnsureCommand( CommandId.DeleteTopic ); }
        }

        public static BookvarCommand AddNotes
        {
            get { return EnsureCommand( CommandId.AddNotes ); }
        }

        public static BookvarCommand SaveNotes
        {
            get { return EnsureCommand( CommandId.SaveNotes ); }
        }

        public static BookvarCommand AddAttachment
        {
            get { return EnsureCommand( CommandId.AddAttachment ); }
        }

        public static BookvarCommand DeleteAttachment
        {
            get { return EnsureCommand( CommandId.DeleteAttachment ); }
        }

        public static BookvarCommand SaveAttachment
        {
            get { return EnsureCommand( CommandId.SaveAttachment ); }
        }

        public static BookvarCommand AddIcon
        {
            get { return EnsureCommand( CommandId.AddIcon ); }
        }

        public static BookvarCommand AddImage
        {
            get { return EnsureCommand( CommandId.AddImage ); }
        }

        public static BookvarCommand AddVideo
        {
            get { return EnsureCommand( CommandId.AddVideo ); }
        }

        public static BookvarCommand DeleteResource
        {
            get { return EnsureCommand( CommandId.DeleteResource ); }
        }

        public static BookvarCommand SaveHyperlink
        {
            get { return EnsureCommand( CommandId.SaveHyperlink ); }
        }

        public static BookvarCommand SwitchMaps
        {
            get { return EnsureCommand( CommandId.SwitchMaps ); }
        }

        public static BookvarCommand NavigateToHyperlink
        {
            get { return EnsureCommand( CommandId.NavigateToHyperlink ); }
        }

        public static BookvarCommand CreateNewMap
        {
            get { return EnsureCommand( CommandId.CreateNewMap ); }
        }

        public static BookvarCommand OpenMap
        {
            get { return EnsureCommand( CommandId.OpenMap ); }
        }

        public static BookvarCommand SaveMap
        {
            get { return EnsureCommand( CommandId.SaveMap ); }
        }

        public static BookvarCommand BalanceMap
        {
            get { return EnsureCommand( CommandId.BalanceMap ); }
        }

        private static BookvarCommand EnsureCommand( CommandId idCommand )
        {
            if ( ( idCommand < 0 ) || ( (int) idCommand >= commandsCount ) )
            {
                return null;
            }
            lock ( internalCommands.SyncRoot )
            {
                if ( internalCommands[ (int) idCommand ] == null )
                {
                    BookvarCommand newCommand =
                        new BookvarCommand( idCommand.ToString(), typeof ( BookvarCommands ) );

                    internalCommands[ (int) idCommand ] = newCommand;
                }
            }
            return internalCommands[ (int) idCommand ];
        }

        private enum CommandId : byte
        {
            AddNewTopic = 0,
            AddNewSubtopic = 1,
            DeleteTopic = 2,
            AddNotes = 3,
            SaveNotes = 4,
            AddAttachment = 5,
            DeleteAttachment = 6,
            SaveAttachment = 7,
            AddIcon = 8,
            AddImage = 9,
            AddVideo = 10,
            DeleteResource = 11,
            SaveHyperlink = 12,
            SwitchMaps = 13,
            CreateNewMap = 14,
            OpenMap = 15,
            SaveMap = 16,
            NavigateToHyperlink = 17,
            AddNewFloatingTopic = 18,
            BalanceMap = 19
        }
    }
}